package com.fzrskj.cloud.free.user.utils;

import com.fzrskj.cloud.free.core.tool.CSessionTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.user.bean.UserLog;
import com.fzrskj.cloud.free.user.dao.UserLogDao;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.support.Session;
import ms.core.tool.StrTool;

import java.util.List;

public class UserLogUtils extends CObject{
	/**
	 * 查询操作日志详情
	 * @param id
	 * @return
	 */
	public UserLog queryLogDetails(long id){
		UserLogDao dao = new UserLogDao();
		UserLog queryLogDetails = dao.queryLogDetails(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryLogDetails;
	}

	/**
	 * 根据条件查询操作日志
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<UserLog> queryuserLog(String userName,String startTime,String endTime,int page,int pageSize,String module,String content){
		UserLogDao dao = new UserLogDao();
		List<UserLog> queryuserLog = dao.queryuserLog(userName, startTime, endTime, page, pageSize, module, content);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryuserLog;
	}

	/**
	 * 根据条件删除操作日志
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public int deluserLog(String userName,String startTime,String endTime,String module, int logTypeId){
		UserLogDao dao = new UserLogDao();
		int i;
		if(logTypeId == 1){
			i = dao.deluserLog(userName, startTime, endTime, module);
		}else if(logTypeId == 2){
			i = dao.delSysLog(startTime, endTime);
		}else{
			dao.dispose();
			this.setErrCode(SysErr.SYS_PARAM_ERR);
			return 0;
		}
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	/**
	 * 根据条件查询总数
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @param module
	 * @return
	 */
	public int queryuserLogCount(String userName,String startTime,String endTime,String module,String content){
		UserLogDao dao = new UserLogDao();
		int count = dao.queryuserLogCount(userName, startTime, endTime, module, content);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return count;
	}

	/**
	 * 添加操作日志
	 * @param module
	 * @param logDesc
	 * @param sid
	 * @return
	 */
	public static int addUserLog(String module, String logDesc, String sid){
		DicParamUtils dicParamUtils = new DicParamUtils();
		String logOpen=dicParamUtils.getParamValue("logOpen", 1);
		if(StrTool.isBlankStr(logOpen)) {
			return 1;
		}

		if(logOpen.equals("1")){
			int utype = CSessionTool.getAccountTypeCloud(sid);
			UserLogDao dao = new UserLogDao();
			String logTypeName = null;
			if(utype==-1){
				logTypeName = "访客";
			}else if(utype==0){
				logTypeName = "管理操作日志";
			}
			else if (utype==1){
				logTypeName = "会员操作日志";
			}
			int uid = CSessionTool.getUidCloud(sid);
			String uName = CSessionTool.getAccountCloud(sid);
			if(uid<=0){
				uName = "访客";
			}

			int ret = dao.addUserLog(module, utype, logTypeName, logDesc, uid, uName);
			String code = dao.getErrCode();
			dao.setErrCode(code);
			dao.dispose();
			return ret;
		}
		return 1;
	}

	/**
	 * 添加日志(用户或管理员操作)
	 * @param module 类指针
	 * @param userId
	 * @param logDesc
	 * @return
	 */
	public static int addLog(Object module,int userId, String methodName, String logDesc){
		logDesc="methodName："+methodName+"，"+logDesc;
		return addLog(module,userId,logDesc);
	}

	public static int addLog(Object module, int userId, String logDesc){
		return addLog(module, userId, logDesc, CloudContextUtils.getAgentId());
	}

	/**
	 * 添加日志(用户或管理员操作)
	 * @param module 类指针
	 * @param userId
	 * @param logDesc
	 * @return
	 */
	public static int addLog(Object module, int userId, String logDesc, Integer agentId){
		String moduleName="";
		String logTypeName="";
		if(module instanceof String) {
			moduleName = String.valueOf(module);
		}else{
			moduleName = module.getClass().getName();
		}

		String userName="";
		int logTypeId=0;
		if(userId==1000){
			userName="admin";
			logTypeId=0;
		}else{
			Session session=CSessionTool.getSession(userId);
			if(session==null) {
				return 0;
			}
			userName=session.getUname();
			logTypeId=session.getUtype();
		}

		if(logTypeId==1){
			logTypeName = "会员操作日志";
		}else if(logTypeId==0){
			logTypeName = "管理操作日志";
		}
		if(userId<=0){
			userName = "访客";
		}
		UserLogDao dao = new UserLogDao();
		int ret = dao.addUserLog(moduleName, logTypeId, logTypeName, logDesc, userId, userName);
		dao.dispose();
		return ret;
	}
}